/*
 * Copyright (c) 2006, 2023, webrx.cn All rights reserved.
 *
 */

package cn;

import java.io.FileWriter;
import java.io.IOException;

/**
 * <p>Project: jse2303 - TryWithDemo
 * <p>Powered by webrx On 2023-07-18 17:37:38
 * <p>描述：<p>
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 17
 */
public class TryWithDemo {
    public static void main(String[] args) {
        //try with resources语句 java 1.7新增的，会自动实现关闭
        // try (对象必须实现AutoCloseEnable接口才可以自动实现关闭，才可以使用try with语句)
        try (var out = new FileWriter("u.txt")) {
            out.write("hello 中文");
        } catch (IOException e) {
            e.printStackTrace();
        }


        FileWriter out = null;
        try {
            out = new FileWriter("u2.txt");
            out.write("hello中文");
            out.flush();

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                out.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
